var db = require('../config/db')
var sql = require('./dbsql/userSql')
var mysql = require('mysql');
/**
 * 用户服务层
 * Author: 梦里小白龙 <jiangwenshu1990@126.com> <http://www.jz07.cn>
 */

//添加用户
const save = (value =>{
    return  new Promise(( resolve, reject ) => {
        db( sql.insert, value).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//验证用户唯一
const checkName = (name=>{
    return  new Promise(( resolve, reject ) => {
        db( sql.checkName, name).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//更新数据
const update = ( params => {
    //console.log("params",params);
    let res = [params.user_name, params.password , params.sex , params.id ];
    return  new Promise(( resolve, reject ) => {
        db( sql.updateUser, res ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//查询用户信息
const getById = ( id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.getByUser, id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})


//删除数据
const udelete = ( user_id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.delUser, user_id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})


//查询用户总数
const count = ( params =>{
    return new Promise(( resolve, reject ) => {
       db( sql.count, ["%"+params.user_name+"%"]).then(data=>{
               if(!data){
                   reject( err )
               }else{
                   resolve(data);
               }
       })
   })  
})


//数据列表
const lists = ( params => {
    console.log("params",params);
    let sum = 0;

    //查询总数
    count(params).then(data=>{
        if(data[0].count > 0){
            sum = data[0].count
        }
    })
    
    var page =  (params.page - 1) * params.size;
    return new Promise(( resolve, reject ) => {
        let arr = ["%"+params.user_name+"%",page,params.size];
        //查询所有信息
        db( sql.lists, arr).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve({data:data,page:params.page,count:sum});
                }
        })
    })
})



//用户登录
const login = ( user_name , password)  => {
	
    return  new Promise(( resolve, reject ) => {
        db( sql.login, [ user_name , password ]).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data[0]);
                }
        })
    })
}

module.exports = {
    save,
    checkName,
    update,
    udelete,
    login,
    lists
}

